package com.jmemoir.authorize.oauth2.controller;

import com.jmemoir.base.common.dto.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录控制层
 *
 * @author Tellsea
 * @date 2023/8/8
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class TestController {

    @GetMapping("/hello")
    public Result<String> hello() {
        return Result.success("Hello");
    }

    @GetMapping("/meet")
    public Result<String> meet() {
        return Result.success("I meet you");
    }

    @GetMapping("/welcome")
    public Result<String> welcome() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return Result.success("Welcome " + authentication.getName());
    }

    @GetMapping("/project")
    @PreAuthorize("hasRole('ROLE_PROJECT_ADMIN')")
    public Result<String> project() {
        return Result.success("This is my project");
    }
}
